import paddle.fluid as fluid
import numpy as np

def reader():
    for i in range(5):
        yield i
shuffled_reader = fluid.io.shuffle(reader, 3)
# for e in shuffled_reader():
#     print(e)

a = np.random.randint(0,30,size=(20,8))
print(a)


b = []
for index in range(20):
    b.append((a[index],np.array(index)))

x = fluid.layers.data(name='x', shape=[8])
y = fluid.layers.data(name='y', shape=[1])
y1 = fluid.layers.fc(input=x,size=3,param_attr='fc_w',bias_attr='fc_b',act=None)

cost = fluid.layers.square_error_cost(input=y1, label=y) #求一个batch的损失值
avg_cost = fluid.layers.mean(cost)
optimizer = fluid.optimizer.SGDOptimizer(learning_rate=0.3)
optimizer.minimize(avg_cost)

use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program()) #Executor的run()方法执行startup_program(),进行参数初始化

feeder = fluid.DataFeeder(place=place, feed_list=[x, y])  # feed_list:向模型输入的变量表或变量表名
out = exe.run(fluid.default_main_program(),
              feed=feeder.feed(b),
              # fetch_list=[avg_cost,y1,'fc_w','fc_b'])
              fetch_list=[y1])
print(out)


